package com.kool.kmetrics.metrics.prometheus;

import com.alibaba.fastjson.JSON;
import com.kool.kmetrics.consumer.vo.SendLog;
import com.kool.kmetrics.metrics.MetricsComputer;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 用户的主题的出站PUBLISH报文数；
 * 客户端的主题的出站PUBLISH报文数；
 * 客户端地址的主题的出站PUBLISH报文数；
 * 用户的各种报文类型的出站报文数、成功数、失败数；
 * 客户端的各种报文类型的出站报文数、成功数、失败数；
 * 客户端地址的各种报文类型的出站报文数、成功数、失败数；
 *
 * @author luyu
 */
@Component
public class SendProm implements MetricsComputer {
    @Resource
    private Counter userSendCnt;

    @Bean
    public Counter userSendCnt() {
        return Counter.build("user_send_cnt", "user send counter")
                .labelNames("userName", "packetType", "success")
                .register(CollectorRegistry.defaultRegistry);
    }


    @Resource
    private Counter userTopicSendCnt;

    @Bean
    public Counter userTopicSendCnt() {
        return Counter.build("user_topic_send_cnt", "user topic send counter")
                .labelNames("userName", "topicName")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter clientSendCnt;

    @Bean
    public Counter clientSendCnt() {
        return Counter.build("client_send_cnt", "client send counter")
                .labelNames("clientId", "packetType", "success")
                .register(CollectorRegistry.defaultRegistry);
    }


    @Resource
    private Counter clientTopicSendCnt;

    @Bean
    public Counter clientTopicSendCnt() {
        return Counter.build("client_topic_send_cnt", "client topic send counter")
                .labelNames("clientId", "topicName")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Resource
    private Counter remoteAddressSendCnt;

    @Bean
    public Counter remoteAddressSendCnt() {
        return Counter.build("remote_address_send_cnt", "remote address send counter")
                .labelNames("remoteAddress", "packetType", "success")
                .register(CollectorRegistry.defaultRegistry);
    }


    @Resource
    private Counter remoteAddressTopicSendCnt;

    @Bean
    public Counter remoteAddressTopicSendCnt() {
        return Counter.build("remote_address_topic_send_cnt", "remote address topic send counter")
                .labelNames("remoteAddress", "topicName")
                .register(CollectorRegistry.defaultRegistry);
    }


    @Override
    public void compute(String log) {
        SendLog sendLog = JSON.parseObject(log, SendLog.class);
        String packetType = sendLog.getPacketType();
        String isSuccess = sendLog.getIsSuccess();
        String userName = sendLog.getUserName();
        String clientId = sendLog.getClientId();
        String remoteAddress = sendLog.getRemoteAddress();
        String topicName = sendLog.getTopicName();

        userSendCnt.labels(userName, packetType, isSuccess).inc();
        clientSendCnt.labels(clientId, packetType, isSuccess).inc();
        remoteAddressSendCnt.labels(remoteAddress, packetType, isSuccess).inc();
        if ("1".equals(isSuccess)) {
            userTopicSendCnt.labels(userName,topicName).inc();
            clientTopicSendCnt.labels(clientId, topicName).inc();
            remoteAddressTopicSendCnt.labels(remoteAddress,topicName).inc();
        }
    }
}
